//
//  BooksController.m
//  LineByLine
//
//  Created by 병철 김 on 11. 12. 10..
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BooksController.h"
#import "CoreDataManager.h"
#import "BookInfoController.h"


@interface BooksController(Private)
- (void) loadBooks;
@end


@implementation BooksController

- (void)dealloc {
    [books release];
    [super dealloc];
}

- (void)loadView {
    [super loadView];
    [self loadBooks];
}

- (void) loadBooks {
    books = [[[CoreDataManager sharedCoreDataManager] selectFrom:@"Book" where:nil] retain];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BookInfoController *controller = [[BookInfoController alloc] init];
    id book = [books objectAtIndex:[indexPath row]];
    [controller setValue:book forKey:@"book"];
    [[self navigationController] pushViewController:controller animated:YES];
    [controller release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"TableViewCell";
    
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
    }
    
    id book = [books objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[book valueForKey:@"title"]];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [books count];
}

@end
